import 'package:FlutterStudy/I10n/localStr.dart';
import 'package:flutter/material.dart';
import 'package:FlutterStudy/generated/intl/messages_all.dart';
import 'package:intl/intl.dart';

import '../generated/l10n.dart';

class i18nDemo extends StatefulWidget {
  @override
  _i18nDemoState createState() {
    return _i18nDemoState();
  }
}

class _i18nDemoState extends State<i18nDemo> {
  late Locale _locale = Localizations.localeOf(context);

  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  // 切换语言的方法
  void _switchLanguage(Locale newLocale) {
    setState(() {
      _locale = newLocale;
      S.load(newLocale);
    });
  }
  @override
  Widget build(BuildContext context) {
    // TODO: implement build
    return Wrap(
      children: [
        Text("${_locale.toString()}"),

        Text(AppLocals.of(context).title!),
        // 中文切换按钮
        TextButton(
            onPressed: () => _switchLanguage(Locale('zh', 'CN')),
            child: Text('中文')
        ),

        // 英文切换按钮
        TextButton(
            onPressed: () => _switchLanguage(Locale('en', 'US')),
            child: Text('English')
        ),
        Divider(),
        Text("intl + flutter intl 插件"),
        Divider(),
        Text("${Intl.defaultLocale}"),

        Text(S.of(context).helloWorld),
        Text(S.of(context).incrementButton),
      ],
    );
  }
}
